package ibm.cognos.imt.chart

import ibm.cognos.imt.Crosstab
import ibm.cognos.imt.Filter
import ibm.cognos.imt.ReportElement
import ibm.cognos.imt.dataUsage.Categories
import ibm.cognos.imt.dataUsage.Measure
import ibm.cognos.imt.dataUsage.Slices
import ibm.cognos.imt.template.AbstractTemplate

/**
 * Created with IntelliJ IDEA.
 * User: eric
 * Date: 1/8/14
 * Time: 6:35 PM
 * To change this template use File | Settings | File Templates.
 */
class PieChart extends ReportElement{
    def legendTitle
    def axisTitle
    def queryName
    def name
    def slices
    def categories
    def measure
    def dataItems
    def owner

    def AbstractTemplate template

    PieChart(report, items) {
        owner = report
        dataItems = items
        items.each {
//            println it.getDataItem().name
            if (it instanceof Slices) {
                this.slices = it.getDataItem().name
            }
            if (it instanceof Categories) {
                this.categories = it.getDataItem().name
            }
            if (it instanceof Measure) {
                this.measure = it.getDataItem().name
                it.getDataItem().aggregate = "total"
            }
        }
    }

    PieChart (node){
        name = node.@name
        queryName = node.@refQuery
        slices = node.pieSlices.chartNodes.chartName.chartNodeMembers.chartNodeMember.@refDataItem
        measure = node.defaultChartMeasure.@refDataItem
        categories = node.pies.chartNodes.chartNode.chartNodeMembers.chartNodeMember.@refDataItem
    }

    def toCombinationChart() {
        CombinationChart chart = new CombinationChart()
        chart.owner = owner
        chart.name = name
        chart.measure = measure
        chart.queryName = queryName
        chart.slices = slices
        chart.categories = categories
        chart
    }

    def toCrosstab() {
        Crosstab ct = new Crosstab()
        ct.owner = owner
        ct.name = name
        ct.measure = measure
        ct.queryName = queryName
        ct.row = slices
        ct.column = categories
        ct
    }

    def filter(exp) {
        def filter = new Filter(exp, owner)
//        update query list of the report
//        ListIterator li = owner.queryList.listIterator()
//        while (li.hasNext()) {
//            def query = li.next()
//            if (query.name == queryName) {
//                li.set(query.addFilter(filter))
//            }
//        }
        owner.queryMap[(queryName)].addFilter(filter)
        this
    }

    def generateSpec(builder) {
        builder.v2_pieChart(maxHotspots: "1000", sliceStartAngle: "90", name: "$name", refQuery: "$queryName") {
            v2_pieTooltips()
            v2_legend {
                v2_legendPosition {
                    v2_legendPreset()

                }
                v2_legendTitle(refQuery: "$queryName") {
                    v2_chartTextContents {
                        v2_automaticText()
                    }
                    style {
                        defaultStyles {
                            defaultStyle(refStyle: "lx")
                        }
                    }
                }
                style {
                    defaultStyles {
                        defaultStyle(refStyle: "lg")
                    }
                }
            }
            v2_axisTitle(refQuery: "$queryName") {
                v2_chartTextContents {
                    v2_automaticText()
                }
                style {
                    defaultStyles {
                        defaultStyle(refStyle: "at")
                    }
                }
            }
            style {
                defaultStyles {
                    defaultStyle(refStyle: "ch")
                }
            }
            noDataHandler {
                contents {
                    block {
                        contents {
                            textItem {
                                dataSource {
                                    staticValue("No Data Available")
                                }
                                style {
                                    CSS(value: "padding:10px 18px;")
                                }
                            }
                        }
                    }
                }
            }
            v2_pieSlices {
                v2_solidPalette {
                    v2_solidPaletteEntries {
                        effects().each {
                            def defaultColor = it[0]
                            def gradientColor = it[1]
                            v2_solidPaletteEntry {
                                v2_fillEffect(defaultColor: "${defaultColor}") {
                                    v2_linearGradient {
                                        v2_gradientColor(gradientColor: "${defaultColor}")
                                        v2_gradientColor(colorPosition: "100", gradientColor: "${gradientColor}")
                                    }
                                }
                            }
                        }
                    }
                }
                chartNodes {
                    chartNode {
                        chartNodeMembers {
                            chartNodeMember(refDataItem: "$slices") {
                                chartContents {
                                    chartTextItem {
                                        dataSource {
                                            memberCaption()
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
            if (categories != null) {
                v2_category {
                    chartNodes {
                        chartNode {
                            chartNodeMembers {
                                chartNodeMember(refDataItem: "$categories")
                            }
                        }
                    }
                }
            }
            v2_defaultChartMeasure(refDataItem: "$measure")
            if (categories != null) {

                v2_matrix {
                    v2_matrixColumns {
                        v2_matrixColumnLabels {
                            style {
                                defaultStyles {
                                    defaultStyle(refStyle: "al")
                                }
                            }
                        }
                        v2_axisTitle(refQuery: "$queryName") {
                            style {
                                defaultStyles {
                                    defaultStyle(refStyle: "at")
                                }
                            }
                            v2_chartTextContents {
                                v2_automaticText()
                            }
                        }
                    }
                }
            }
        }
    }

    def effects() {
        [
                ["#8599D3", "#5876AE"],
                ["#E3AE6C", "#CD854E"],
                ["#839862", "#6C7F56"],
                ["#B7C873", "#AFB885"],
                ["#8484A8", "#525E7E"],
                ["#C0CCED", "#B0C2E5"],
                ["#8C5580", "#794067"],
                ["#C789BC", "#BB72BC"],
                ["#D5BAEF", "#C29FD1"],
                ["#83683F", "#604926"],
                ["#DCB05A", "#C09C52"],
                ["#F4DF9E", "#E4CF87"],
                ["#5F8A8C", "#537579"],
                ["#96C4B2", "#89B0A0"],
                ["#CBE8E7", "#BDD6D5"],
                ["#AE6564", "#875352"],
                ["#D88C6F", "#C47D61"],
                ["#E3C9B0", "#D2B2A5"],
                ["#848484", "#555555"],
                ["#a4a4a4", "#909090"],
                ["#C7C7C7", "#c1c1c1"]
        ]
    }

}

